#include "speedreader.h"

SpeedReader::SpeedReader(TextFormatReader *pTextFormatReader)
{
    this->pTextFormatReader = pTextFormatReader;

    connect(pTextFormatReader, SIGNAL(error(SpeedReaderError)), this, SIGNAL(error(SpeedReaderError)));
    connect(pTextFormatReader, SIGNAL(maxWordLength(int)), this, SLOT(setMaxWordLength(int)));
    connect(pTextFormatReader, SIGNAL(maxWordLength(int)), this, SIGNAL(wordOffset(int)));

    this->wordsCount = -1;
    this->currentPosition = -1;
    this->readPositionPercent = -1;
    this->commaPauseTime = 500;
    this->dotPauseTime = 1000;
    this->wordPerMinute = 200;
    this->maxWordLength = 40;
    this->shiftingEnabled = true;

    connect(&pauseTimer, SIGNAL(timeout()), this, SLOT(startReading()));
    connect(&readingTimer, SIGNAL(timeout()), this, SLOT(readWord()));
}

void SpeedReader::readWord()
{
    this->readPositionPercent = floor((double)(this->currentPosition + 1) / this->wordsCount * 100 + 0.5) / 100;

    if (this->currentPosition < this->wordsCount)
    {
        QString nextWord = this->words.at(this->currentPosition);
        int shift = 0;

        if (this->shiftingEnabled)
        {
            shift = this->maxWordLength - nextWord.count() / 3;

            QString spaces;

            for (int i = 0; i < shift; i++)
            {
                spaces.append(" ");
            }

            nextWord = nextWord.prepend(spaces);
        }

        emit this->nextWordAvailable(nextWord, shift);
        emit this->readingProgress(this->readPositionPercent);

        if (nextWord.contains(","))
        {
            readingTimer.stop();
            pauseTimer.start(this->commaPauseTime);
        } else
        {
            if (nextWord.contains(".") || nextWord.contains("!") || nextWord.contains("?"))
            {
                readingTimer.stop();
                pauseTimer.start(this->dotPauseTime);
            }
        }
    } else
    {
        this->stopReading();

        emit this->endOfBook();
    }

    this->currentPosition++;
}

void SpeedReader::setWordsToRead(const QStringList &words)
{
    this->words = words;
    this->wordsCount = this->words.count();
}

void SpeedReader::setCurrentPosition(const int &currentPosition)
{
    pTextFormatReader->openBook(pTextFormatReader->getFileName());
    this->setWordsToRead(pTextFormatReader->getWords());

    if (currentPosition < this->wordsCount)
    {
        this->currentPosition = currentPosition;
    }
}

void SpeedReader::setCommaPauseTime(const int &commaPauseTime)
{
    this->commaPauseTime = commaPauseTime;
}

void SpeedReader::setDotPauseTime(const int &dotPauseTime)
{
    this->dotPauseTime = dotPauseTime;
}

void SpeedReader::setReadingSpeed(int wordPerMinute)
{
    this->wordPerMinute = wordPerMinute;
}

void SpeedReader::setMaxWordLength(const int &maxWordLength)
{
    this->maxWordLength = maxWordLength;
}

void SpeedReader::setShiftingEnabled(const bool &shiftingEnabled)
{
    this->shiftingEnabled = shiftingEnabled;
}

QStringList SpeedReader::getWordsToRead() const
{
    return this->words;
}

int SpeedReader::getCurrentPosition() const
{
    return this->currentPosition;
}

int SpeedReader::getCommaPauseTime() const
{
    return this->commaPauseTime;
}

int SpeedReader::getDotPauseTime() const
{
    return this->dotPauseTime;
}

int SpeedReader::getReadingSpeed() const
{
    return this->wordPerMinute;
}

int SpeedReader::getWordsCount() const
{
    return this->wordsCount;
}

bool SpeedReader::isShiftingEnabled() const
{
    return this->shiftingEnabled;
}

void SpeedReader::startReading()
{
    switch (this->getCurrentPosition())
    {
        case -1:
        {
            emit this->error(UndefinedCurrentPositionError);

            break;
        }

        default:
        {
            if (!words.isEmpty())
            {
                pauseTimer.stop();

                readingTimer.start(60000 / this->wordPerMinute);
            }

            break;
        }
    }


}

void SpeedReader::stopReading()
{
    pauseTimer.stop();
    readingTimer.stop();
}
